package controllerLayer;
import java.util.List;

import modelLayer.*;
import BDLayer.*;

public class PalletController {

	
	private IFDBPallet palletDB = new DBPallet();
	
	
	
	
	//interaction 5
	//get all pallet by productID
	public List<Pallet> getAllPalletByProductID(String productID){
		return palletDB.findPalletByProductID(productID, false);
	}
	
	
	//get all Pallet
	public List<Pallet> getAllPallet(){
		return palletDB.getAllPallet(false);
	}
	
	//get Pallet by ID
	public Pallet findPalletByID(int palletID){
		return palletDB.findPalletByID(palletID, false);
	}
	
	
	
	
	
	public boolean createPallet(String batchNo, int noOfPlates, int supplierFail, int statusOnStore, String storeRef, String prID, int personID)	{
		boolean ok = false;
			try{
				Pallet paObj = new Pallet();
				paObj.setBatchNo(batchNo);
				paObj.setNumberOfPlates(noOfPlates);
				paObj.setSupplierFail(supplierFail);
				paObj.setStatusOnStore(statusOnStore);
				paObj.setStore(new Store(storeRef));
				paObj.setProduct(new Product(prID));	
				Person person = new StoreEmployee();
				person.setId(personID);
				paObj.setpObj(person);
				
				if(insertPallet(paObj) == 1){
					ok = true;
				}
			}
			catch(Exception e){
				p("Exception creating pallet Object in PalletController : "+e);
			}
			return ok;
	}
	
	
	
	
	
	//insert pallet into DB
	public int insertPallet(Pallet paObj){
		int rc = -1;		
		try{
			rc = palletDB.insertPallet(paObj, true);	
			
		}
		catch(Exception e){
			p("Exception insert Pallet :"+e);
		}
		return rc;
	}
	
	
	
	
	public boolean updatePallet(Pallet paObj){
		boolean saved = false;		
		try{
			palletDB.updatePallet(paObj, false);
			saved = true;
		}
		catch(Exception e){
			p(e.toString());
		}
		return saved;
	}
	
	
	public int deletePallet(int paID){
		Pallet paObj = findPalletByID(paID);
		
		int rc = -1;
		try {
			rc = palletDB.deletePallet(paObj, false);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rc;
	}
	
	
	
	
	
	public void p(String e){System.out.println(e);}
}
